package com.ferryc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ferryc.entity.Order;
import com.ferryc.entity.OrderDetail;
import com.ferryc.entity.Products;
import com.ferryc.entity.Stroage;
import com.ferryc.exception.HandleException;
import com.ferryc.exception.NSFException;
import com.ferryc.exception.UnderstockException;
import com.ferryc.fegin.PayFeignClient;
import com.ferryc.fegin.StroageFeignClient;
import com.ferryc.mapper.IOrderMapper;
import com.ferryc.mapper.OrderDetailMapper;
import com.ferryc.model.Product;
import com.ferryc.model.Result;
import com.ferryc.service.IOrderService;
import com.ferryc.service.IProductsService;
import com.ferryc.utils.DigitalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Ferry Chen on 2019/11/1.
 */
@Service
public class OrderServiceImpl extends ServiceImpl<IOrderMapper, Order> implements IOrderService {
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IProductsService productsService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private StroageFeignClient stroageFeignClient;
  // @Autowired
  // private PayFeignClient payFeignClient;


    @Override
    @Transactional
    public int saveOrder(String userId, List<Product> productsList) {
        Order o = new Order();
        String orderId = UUID.randomUUID().toString();
        List<OrderDetail> details = new ArrayList<>();
        OrderDetail detail = null;
        int count = 0;
        double money = 0;
        //检查库存

        //检查余额
        for (Product product : productsList) {
            Products products = productsService.getById(product.getProductId());
            if (products != null) {
                 Result pr = stroageFeignClient.getByProductId(products.getId());
                 if (Result.check(pr)) {
                     throw new HandleException();
                 }
                 Stroage stroage = (Stroage) pr.getData();
                 if (stroage.getCount() < product.getCount()) {
                     throw new UnderstockException();
                 }
                double total = DigitalUtils.multiply(products.getPrice(), product.getCount());
                money = DigitalUtils.add(money, total);
                detail = new OrderDetail();
                detail.setOrderId(orderId);
                detail.setProductId(products.getId());
                detail.setCount(product.getCount());
                detail.setPrice(products.getPrice());
                detail.setTotalPrice(total);
                detail.setInsertTime(new Date());
                detail.setUpdateTime(new Date());
                orderDetailMapper.insert(detail);
                stroageFeignClient.consume(products.getId(),product.getCount());
                count += product.getCount();
                detail = null;
            } else {
                throw new HandleException("商品不存在");
            }
        }
        /*Result pr = payFeignClient.checkBalance(userId, money);
        if (Result.check(pr)) {
            throw new NSFException();
        }*/
        o.setId(orderId);
        o.setUserId(userId);
        o.setCount(count);
        o.setMoney(money);
        o.setInsertTime(new Date());
        o.setUpdateTime(new Date());
        orderMapper.insert(o);
     //   payFeignClient.consume(userId,money);
        return 1;
    }
}
